package com.woniuxy.dao.impl;

import com.woniuxy.dao.StudentDao;
import com.woniuxy.entity.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 作者
 * @create 2025-09-17 10:51
 */
public class StudentDaoImpl implements StudentDao {

    String path = "d:\\aaa\\students.txt";

    @Override
    public void insert(Student student) {
        //将学生的属性写入文件中
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(path, true));
            bw.write(student.getSid() + "-");
            bw.write(student.getSname() + "-");
            bw.write(student.getPassword() + "-");
            bw.write(student.getAge() + "-");
            bw.write(student.getEmail() + "-");
            bw.write(student.getPhone());
            bw.newLine();
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectBySname(String sname) {
        try {
            Student student = null;
            BufferedReader br = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = br.readLine()) != null) {
                //sid-sname-password-age-email-phone
                String[] split = line.split("-");
                if (split[1].equals(sname)) {
                    student = new Student();
                    student.setSid(Integer.parseInt(split[0]));
                    student.setSname(split[1]);
                    student.setPassword(split[2]);
                    student.setAge(Integer.parseInt(split[3]));
                    student.setEmail(split[4]);
                    student.setPhone(split[5]);
                }
            }
            return student;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectByEmail(String email) {
        try {
            Student student = null;
            BufferedReader br = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = br.readLine()) != null) {
                //sid-sname-password-age-email-phone
                String[] split = line.split("-");
                if (split[4].equals(email)) {
                    student = new Student();
                    student.setSid(Integer.parseInt(split[0]));
                    student.setSname(split[1]);
                    student.setPassword(split[2]);
                    student.setAge(Integer.parseInt(split[3]));
                    student.setEmail(split[4]);
                    student.setPhone(split[5]);
                }
            }
            return student;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Student> selectAll() {
        try {
            List<Student> students = new ArrayList<Student>();
            BufferedReader br = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = br.readLine()) != null) {
                Student student = new Student();
                String[] split = line.split("-");
                student = new Student();
                student.setSid(Integer.parseInt(split[0]));
                student.setSname(split[1]);
                student.setPassword(split[2]);
                student.setAge(Integer.parseInt(split[3]));
                student.setEmail(split[4]);
                student.setPhone(split[5]);
                students.add(student);
            }
            return students;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insertAll(List<Student> students) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(path));
            for(Student student : students) {
                bw.write(student.getSid() + "-");
                bw.write(student.getSname() + "-");
                bw.write(student.getPassword() + "-");
                bw.write(student.getAge() + "-");
                bw.write(student.getEmail() + "-");
                bw.write(student.getPhone());
                bw.newLine();
                bw.flush();
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public Student selectBySid(int sid) {
        try {
            Student student = null;
            BufferedReader br = new BufferedReader(new FileReader(path));
            String line = null;
            while ((line = br.readLine()) != null) {
                //sid-sname-password-age-email-phone
                String[] split = line.split("-");
                if (split[0].equals(sid+"")) {
                    student = new Student();
                    student.setSid(Integer.parseInt(split[0]));
                    student.setSname(split[1]);
                    student.setPassword(split[2]);
                    student.setAge(Integer.parseInt(split[3]));
                    student.setEmail(split[4]);
                    student.setPhone(split[5]);
                }
            }
            return student;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}
